package com.example.lz.recycleviewdemo;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.Random;


public class MainActivity extends AppCompatActivity {
    private final String TAG = "MainActivity";
    private RecyclerView mRecyclerView;
    private RecycleAdapter mAdapter;
    private int itemCount = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar supportActionBar = getSupportActionBar();
        assert supportActionBar != null;
        supportActionBar.setHomeAsUpIndicator(R.mipmap.ic_launcher);


        initView();
    }

    private void initView() {
        final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        assert fab != null;
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                GridLayoutManager layoutManager = (GridLayoutManager) mRecyclerView.getLayoutManager();
                int count = layoutManager.getItemCount();
                if (count == 0) {
                    return;
                }

                Random random = new Random();
                int position = random.nextInt(count);
                itemCount--;
                mAdapter.notifyItemRemoved(position);

                count = layoutManager.getItemCount();
                //删除Item时 刷新底部Item
                int spanCount = layoutManager.getSpanCount();
                int startPostion;
                if (count % spanCount == 0) {
                    startPostion = count - spanCount;
                } else {
                    startPostion = count / spanCount * spanCount;
                }
                mAdapter.notifyItemRangeChanged(startPostion, count - startPostion + 1);
            }
        });

        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        mRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        mAdapter = new RecycleAdapter();
        mRecyclerView.setAdapter(mAdapter);
        int space = (int) getResources().getDimension(R.dimen.recycle_view_space);
        mRecyclerView.addItemDecoration(new RecycleViewItemDecoration(space, space, true));
    }


    class RecycleAdapter extends RecyclerView.Adapter<RecycleHolderView> {

        @Override
        public RecycleHolderView onCreateViewHolder(final ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).
                    inflate(R.layout.item_recycleview, null, false);
            return new RecycleHolderView(view);
        }

        @Override
        public void onBindViewHolder(RecycleHolderView holder, int position) {
            holder.image.setBackgroundColor(getResources().getColor(R.color.colorAccent));
            holder.image.setImageResource(R.mipmap.ic_launcher);
        }

        @Override
        public int getItemCount() {
            return itemCount;
        }
    }

    class RecycleHolderView extends RecyclerView.ViewHolder {
        ImageView image;

        public RecycleHolderView(View view) {
            super(view);
            this.image = (ImageView) view.findViewById(R.id.imageview_recycle);
        }
    }
}